/*
 * MandatoryValidator.java
 * 
 * Created on 21-Aug-2007, 07:58:36
 * 
 * Copyright (C)
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */

package com.grubby.databean.constraints.validation;

import com.grubby.databean.constraints.annotation.Mandatory;
import java.lang.annotation.Annotation;

/**
 * A validator for the {@link Mandatory Mandatory} constraint.
 * <p>
 * Note this has been adapted from an Annotation processing example created by 
 * Anders Holmgren of Sun Microsystems
 * </p>
 * @see Mandatory
 *
 * @author Anders Holmgren, with alterations by A. HARMEL-LAW. 
 * E: andrew.harmel.law@gmail.com
 * @version 0.1
 */
public class MandatoryValidator extends AbstractConstraintValidator {

    /**
     * Constructs a MandatoryValidator object.
     */
    // suppress the unchecked warning for unchecked generic array creation
    @SuppressWarnings("unchecked")
    public MandatoryValidator() {
        super(Mandatory.class);
    }

    public ConstraintViolation validate(Object objValue, 
            Annotation constraint) {
        
        ensureConstraintIsSupported(constraint);

        /*
         * If the value passed in came from a primitive property then it will
         * never be null anyway, so there is no need to attempt to determine
         * whether the constraint was applied to a primitive type.
         */
        ConstraintViolation result = null;

        if (objValue == null) {
            System.out.println("The constraint was violated");
            result = new ConstraintViolation(constraint,
                "Mandatory constraint violated.");
        }

        return result;
    }
}